
const htp = 'http://47.99.96.177/cocps/userServer'
var mdk = require('../utils/md5.js');

const app = getApp();
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function checkPhone(t) {
  if (!(/^1(3|4|5|7|8)\d{9}$/.test(t))) {
    return false;
  }
  return true;
}

function httServer(port, jsonParm, callback) {
  var dic = jsonParm;
  var nocer_no = randomWord(false,32);
  dic['NONCE_STR'] = nocer_no;
  console.log('随机数'+nocer_no);
  var sdic = Object.keys(dic).sort();
  var tempstr = '';
  for (var ki in sdic) {
    console.log(sdic[ki] + ":" + dic[sdic[ki]] + ",");
    var temp = dic[sdic[ki]];
    if (temp == '' || !temp) {
      continue;
    }
    tempstr += sdic[ki] + "=" + dic[sdic[ki]] + '&';
  }
  tempstr = tempstr.replace(/\s+/g, "");
  tempstr = tempstr.substring(0, tempstr.length - 1)
  tempstr = tempstr +'&KEY=79HTJXFW29B6'
  console.log(tempstr);
  var secr = mdk.hexMD5(tempstr);
  dic['SIGN'] = secr.toUpperCase();
  var jsdic = { 'ACTION_NAME': port,'ACTION_INFO':dic};
  wx.request({
    url: htp,
    method: 'POST',
    header: {
      'content-type': 'application/json' // 默认值
    },
    data: jsdic,
    success(res) {
      var rdta = res.data;
      var rcode = res.statusCode;
      if (rcode == '200') {
        if (rdta.ACTION_RETURN_CODE == '000000') {
          callback('', rdta.ACTION_INFO);
        } else {
          callback(rdta.MESSAGE);
        }
      } else {
        callback('请求失败,返回:' + rcode);
      }

    },
    fail(e) {
      callback('请求失败')
    }
  })
}
//带token请求
function serverToken(port, jsonParm, callback) {
  var dic = jsonParm;
  var nocer_no = randomWord(false, 32);
  dic['NONCE_STR'] = nocer_no;
  var userid = app.globalData.userid;
  var sessonid = app.globalData.sessonid;
  dic['SESSION_ID']=sessonid;
  dic['USER_ID'] = userid;
  console.log('随机数' + nocer_no);
  var sdic = Object.keys(dic).sort();
  var tempstr = '';
  for (var ki in sdic) {
    console.log(sdic[ki] + ":" + dic[sdic[ki]] + ",");
    var temp = dic[sdic[ki]];
    if (temp == '' || !temp) {
      continue;
    }
    tempstr += sdic[ki] + "=" + dic[sdic[ki]] + '&';
  }
  tempstr = tempstr.replace(/\s+/g, "");
  tempstr = tempstr.substring(0, tempstr.length - 1)
  tempstr = tempstr + '&KEY=79HTJXFW29B6'
  console.log(tempstr);
  var secr = mdk.hexMD5(tempstr);
  dic['SIGN'] = secr.toUpperCase();
  var jsdic = { 'ACTION_NAME': port, 'ACTION_INFO': dic };
  wx.request({
    url: htp,
    method: 'POST',
    header: {
      'content-type': 'application/json' // 默认值
    },
    data: jsdic,
    success(res) {
      var rdta = res.data;
      var rcode = res.statusCode;
      if (rcode == '200') {
        if (rdta.ACTION_RETURN_CODE == '000000') {
          callback('', rdta.ACTION_INFO);
        } else {
          callback(rdta.MESSAGE);
        }
      } else {
        callback('请求失败,返回:' + rcode);
      }

    },
    fail(e) {
      callback('请求失败')
    }
  })
}
//生成随机数
// 生成3 - 32位随机串：randomWord(true, 3, 32)
// 生成43位随机串：randomWord(false, 43)
function randomWord(randomFlag, min, max) {
  var str = "",
    range = min,
    arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

  // 随机产生
  if (randomFlag) {
    range = Math.round(Math.random() * (max - min)) + min;
  }
  for (var i = 0; i < range; i++) {
  var  pos = Math.round(Math.random() * (arr.length - 1));
    str += arr[pos];
  }
  return str;
}

//用户登录状态判断
function userStatus(rcode){
  var result = '';
   if(rcode =='10'){
     result = '去认证';
   }else if(rcode == '20'){
     result = '待核实';
   }else if(rcode == '30'){
     result = '待认证';
   }else if(rcode == '40'){
     result = '开通绑定'
   }else if(rcode == '50'){
     result = '锁定用户'
   }else if(rcode == '60'){
     result = '已认证';
   }else if(rcode == '99'){
     result = '注销用户'
   }else{
     result = '状态异常';
   }
   return result
}
//用户等级
function userlevel(rcode){
  var result = '';
  if (rcode == '100001'){
    result = '普通用户'
  } else if (rcode =='100002'){
    result = '高级会员 '
  }else{
    result = '至尊会员'
  }
  return result;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime,
  checkPhone: checkPhone,
  httServer: httServer,
  serverToken: serverToken,
  userStatus: userStatus,
  userlevel: userlevel,
  randomWord: randomWord,
}